# 
# 就去看看书 网 https://www.97kks.com


import scrapy
from downloader.items import kksItem
from scrapy.http import Request

class KksSpider(scrapy.Spider):
    
    name = 'kks'

    allowed_domains = ["www.97kks.com"]
    start_urls = [
        "https://www.97kks.com/html/60/60261/10960888.html"
    ]

    bookname = '骑士悲歌.txt'

    def parse(self, response):

        title = response.xpath("//dd/h1/text()").extract()[0]
        # title = title.replace(' ', '章 ')
        # title = title.replace('章 第', '第')
        
        contentarray = response.xpath("//dd[@id='contents']/text()").extract()
        text = ''
        for substr in contentarray:
            text = text + substr

        item = kksItem()
        item['book'] = self.bookname
        item['content'] = text
        item['title'] = title
        
        yield item

        next_url = response.xpath("//dd[@id='footlink']/a[last()]/@href").extract()[0]
        if len(next_url) > 0 :
            next_url = 'https://www.97kks.com' + next_url
            yield Request(next_url, callback=self.parse)
        else :
            print('\n\n*************书本下载完毕 ！！！*************')
            


        